using System;
using System.Data;

namespace sprocgen.Entities
{
    public class ParameterConfig
    {
        public int ID;
        private string m_Name;
        
        public string Name
        {
            get { return m_Name; }
            set
            {
                m_Name = value;
                UpdateNames();
            }
        }

        private string m_DataType;
        public string DataType
        {
            get { return m_DataType; }
            set
            {
                m_DataType = value;
                UpdateDataTypes();
            }
        }
        
        public int? Length;
        public bool IsOutParameter;
        public bool HasDefaultValue;

        private string m_FriendlyDataType;
        private SqlDbType m_SqlDbType;

        public string FriendlyDataType { get { return m_FriendlyDataType; } }
        public SqlDbType SqlDbType { get { return m_SqlDbType; } }

        private string m_PascalName;
        private string m_CamelName;

        public string PascalName { get { return m_PascalName; } }
        public string CamelName { get { return m_CamelName; } }

        private void UpdateNames()
        {
            m_PascalName = m_Name.Replace("@", "");
            if (!char.IsLetter(m_PascalName[0]) && m_PascalName[0] != '_')
                m_PascalName = "_" + m_PascalName;
            m_CamelName = m_PascalName;
            if (m_CamelName.Length >= 2)
            {
                if (char.IsUpper(m_CamelName[0]) && char.IsLower(m_CamelName[1]))
                {
                    m_CamelName = char.ToLower(m_CamelName[0]) + m_CamelName.Substring(1);
                }
            }
        }

        private void UpdateDataTypes()
        {
            Type paramType;

            switch (DataType)
            {
                case "int":
                    paramType = typeof(int?);
                    m_FriendlyDataType = "int?";
                    m_SqlDbType = SqlDbType.Int;
                    break;

                case "varchar":
                    paramType = typeof(string);
                    m_FriendlyDataType = "string";
                    m_SqlDbType = SqlDbType.VarChar;
                    break;

                case "nvarchar":
                    paramType = typeof(string);
                    m_FriendlyDataType = "string";
                    m_SqlDbType = SqlDbType.NVarChar;
                    break;

                case "bit":
                    paramType = typeof(bool?);
                    m_FriendlyDataType = "bool?";
                    m_SqlDbType = SqlDbType.Bit;
                    break;

                case "sysname": // todo: not sure about this one
                    paramType = typeof(string);
                    m_FriendlyDataType = "string";
                    m_SqlDbType = SqlDbType.VarChar;
                    break;

                case "datetime":
                    paramType = typeof(DateTime?);
                    m_FriendlyDataType = "DateTime?";
                    m_SqlDbType = SqlDbType.DateTime;
                    break;

                case "money":
                    paramType = typeof(decimal?);
                    m_FriendlyDataType = "decimal?";
                    m_SqlDbType = SqlDbType.Money;
                    break;

                case "tinyint":
                    paramType = typeof(int?);
                    m_FriendlyDataType = "int?";
                    m_SqlDbType = SqlDbType.TinyInt;
                    break;

                case "smallint":
                    paramType = typeof(int?);
                    m_FriendlyDataType = "int?";
                    m_SqlDbType = SqlDbType.SmallInt;
                    break;

                case "char":
                    paramType = typeof(string);
                    m_FriendlyDataType = "string";
                    m_SqlDbType = SqlDbType.Char;
                    break;

                case "nchar":
                    paramType = typeof(string);
                    m_FriendlyDataType = "string";
                    m_SqlDbType = SqlDbType.NChar;
                    break;

                case "ntext":
                    paramType = typeof(string);
                    m_FriendlyDataType = "string";
                    m_SqlDbType = SqlDbType.NText;
                    break;

                case "text":
                    paramType = typeof(string);
                    m_FriendlyDataType = "string";
                    m_SqlDbType = SqlDbType.Text;
                    break;

                default:
                    throw new Exception(string.Format("Unhandled data type: {0}", DataType));
            }
        }
    }
}
